package cn.harry.modular.sys.service.impl;

import cn.harry.modular.sys.mapper.SysRoleMenuMapper;
import cn.harry.modular.sys.domain.SysRoleMenu;
import cn.harry.modular.sys.service.SysRoleMenuService;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * 后台用户角色和权限关系表
 *
 * @author honghh Date 2019-10-12 10:55:44 Copyright (C) www.tech-harry.cn
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu>
		implements SysRoleMenuService {

	@Override
	public void deleteByMenuId(Long menuId) {
		LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(SysRoleMenu::getMenuId, menuId);
		this.baseMapper.delete(wrapper);
	}

	@Override
	public List<Long> listMenuIdByRoleId(Long roleId) {
		List<Long> ids = new ArrayList<>();
		LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(SysRoleMenu::getRoleId, roleId);
		List<SysRoleMenu> sysRoleMenus = list(wrapper);
		if (CollectionUtil.isNotEmpty(sysRoleMenus)) {
			sysRoleMenus.forEach(item -> {
				ids.add(item.getMenuId());
			});
			return ids;
		}
		return null;
	}

	@Override
	public long checkMenuExistRole(Long menuId) {
		return count(
				new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getMenuId, menuId));
	}

	@Override
	public void insertRoleAndRoleMenu(Long id, List<Long> menuIds) {
		this.baseMapper.insertRoleAndRoleMenu(id, menuIds);
	}

}